﻿@{
    ViewBag.Title = "教学资源";
    Layout = "~/Views/Shared/_GridCommonLayout.cshtml";
}
<link rel="Stylesheet" href="~/Scripts/Z-tree/css/zTreeStyle/zTreeStyle.css" />
<link href="~/CSS/font-awesome-4.2.0/css/font-awesome.css" rel="stylesheet" />
<style type="text/css">
    .folder {
        font-size: 18px;
        margin: 5px;
        color: #EE9A00;
    }

    .file {
        font-size: 18px;
        margin: 5px;
        color: #4876FF;
    }

    .tag {
        font-size: 15px;
        color: blue;
    }

    .red {
        color: red;
    }

    .green {
        color: green;
    }

    .iconSize i {
        font-size: 24px;
    }
</style>
<!--begin grid code-->
<section id="widget-grid" class="">
    <div class="row">


        <!--Operation-->
        <div class="heading-buttons">


            <div class="col-xs-12 col-sm-7 col-md-7 col-lg-4">
                <h1 class="page-title txt-color-blueDark"><i class="fa fa-cloud fa-fw " style="color: blue;"></i>
                    教学资源		<span style="font-size: 15px; color: #cccccc; margin-left: 30px;">当前课程 >> @ViewBag.CourseName</span>
                </h1>
            </div>
            <div style="clear: both;">
            </div>
        </div>
        <!--/Operation-->



        <div>
            <div class="buttons pull-left">

                <a id="resource" href="#myModal" role="button" class="btn btn-success btn-labeled" data-toggle="modal" style="margin-left: 11px;">
                    <span class="btn-label"><i class="glyphicon glyphicon-log-in"></i>
                    </span>导入资源</a>

                <button type="button" class="btn btn-trash btn-labeled "
                    data-bind="click: $root.showAddBefore" style="margin-left: 10px;">
                    <span class="btn-label"><i class="glyphicon glyphicon-plus"></i>
                    </span>新建文件夹</button>

                <button type="button" class="btn btn-trash btn-labeled "
                    data-bind="click: $root.onDataDeleting" style="margin: 5px;">
                    <span class="btn-label"><i class="glyphicon glyphicon-trash"></i>
                    </span>删除文件</button>

            </div>
            <!--Search-->
            <div class="filter-bar pull-left" style="background-color: white; border: none;">
                <form id="formSearch">

                    <button type="button" class="btn btn-trash btn-labeled"
                        data-bind="click: $root.search" style="margin: 5px;">
                        <span class="btn-label"><i class="glyphicon glyphicon-search"></i>
                        </span>查找文件</button>

                    <div>
                        <div class='input-append'>

                            <input id='SourceName' name='SourceName' type='text' class='input-mini' placeholder="文件名" style='width: 130px; height: 30px; margin-left: 0;' />
                        </div>
                    </div>
                    <input type="hidden" id="ID" name="ID" data-bind="value:$root.ID" />
                    <input type="hidden" id="IParentID" name="IParentID" data-bind="value:$root.IParentID" />
                    <input type="hidden" id="CouurseID" name="CourseID" value="@ViewBag.CourseID">
                    <div class="clearfix">
                    </div>
                </form>
            </div>
            <!--/search-->
        </div>



        <article class="col-xs-12 col-sm-12 col-md-12 col-lg-12 sortable-grid ui-sortable">
            <div id="wid-id-0" class="jarviswidget jarviswidget-color-darken jarviswidget-sortable" data-widget-editbutton="false">
                <header style="background-color: #D4D4D4; border: none;">
                    <span class="widget-icon"><i class="fa fa-table"></i></span>
                    <a href="#" data-bind="click:$root.GoToParent" style="margin-left: 10px" id="GoToParent" hidden>
                        <h2 style="color: black; font-size: 16px;">| 返回上一级</h2>
                    </a>
                    <h2 style="color: black; font-size: 16px;">全部文件</h2>
                </header>

                <div style="margin-top: 35px;">

                    <div class="jarviswidget-editbox"></div>
                    <div class="widget-body no-padding">
                        <div role="grid" id="providerGrid" class="dataTables_wrapper form-inline">
                            <div class="table-responsive">

                                <div class="dt-wrapper" style="overflow-y: auto;">
                                    <table class="table table-striped table-bordered table-hover checkboxs">
                                        <!--table head-->
                                        <thead>
                                            <tr>
                                                <th style="width: 1%">
                                                    <input type="checkbox" value="0" />
                                                </th>
                                                <!-- ko foreach: headers -->
                                                <th class="left">
                                                    <a href="#" data-bind="text: displayText@*, click: $root.sort*@" style="color: black"></a>
                                                    <i class="icon-circle-arrow-up" data-bind="    visible: value == $root.orderBy() && $root.isAsc()"></i>
                                                    <i class="icon-circle-arrow-down" data-bind="    visible: value == $root.orderBy() && !$root.isAsc()"></i>
                                                </th>
                                                <!-- /ko -->
                                                <th class="center" style="width: 100px">操作
                                                </th>
                                            </tr>
                                        </thead>
                                        <!--/table head-->
                                        <!--table body(Provider list)-->
                                        <tbody data-bind="foreach: recordSet">
                                            <tr class="selectable" onclick="">
                                                <td>
                                                    <input type="checkbox" name="userSelector" data-bind="value:$root.typeAndId(Id,FileType)" />
                                                    <input type="hidden" id="recordParent" data-bind='value: IParentId' />
                                                    <input type="hidden" id="recordILevel" data-bind='value: ILevel' />
                                                </td>

                                                <td class="left">
                                                    <a href="#" style="color: black" data-bind='click:$root.GetFile' class="iconSize">
                                                        <i data-bind="css:$root.showFileType(FileType,IType)"></i>
                                                        <strong data-bind='text: SSourceName'></strong></a>
                                                </td>

                                                <td class="left"><strong data-bind='text: Size(ISize)'></strong></td>

                                                <td class="left">
                                                    <button class="btn btn-link" data-bind='css:$root.fontColor(IPublic),text: PublicState(IPublic),click:$root.ChangeState'></button>
                                                </td>

                                                <td class="left"><i data-bind="css:$root.showImg(IIsDownload)"></i>
                                                    <button class="btn btn-link" data-bind='text: DownloadPower(IIsDownload),click:$root.ChangePower'></button>
                                                </td>

                                                <td class="left">
                                                    <a href="#" data-bind="click: $root.showUpdateModal">
                                                        <i class="tag fa fa-pencil" title="重命名"></i></a>
                                                    &nbsp; &nbsp;
                                                    <a data-bind="attr: { href: SUrl}"><i data-bind="css:$root.showUploadImag(FileType)" title="下载"></i></a>
                                                    &nbsp; &nbsp;
                                                    <a href="#" data-bind="click: $root.ChangeView">
                                                        <i data-bind="css:$root.showPreImag(FileType,IType)" title="预览"></i></a>
                                                </td>
                                            </tr>
                                        </tbody>
                                        <!--/table body( Provider list)-->

                                    </table>

                                </div>


                                <!-- 分页 -->
                                <ul class="pagination pagination-alt">
                                </ul>

                                <div class="dt-top-row total  ">
                                </div>
                                <!--end 分页-->
                            </div>

                            <!-- /分页 -->

                        </div>

                        <!-- /分页 -->


                    </div>
                </div>

            </div>

        </article>

    </div>
    <!--end row-->
</section>


<!--TeaSource详细对话框-->
<div id="TeaSourceMessageModal" class="modal fade" id="ProviderMessageModal" tabindex="-1" role="dialog"
    aria-labelledby="TeaSourceMessageModalLabel" aria-hidden="true">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                ×</button>
            <h3>设置文件
            </h3>
        </div>

        <div class="modal-body">
            <input type='hidden' id='txtbID' name='txtbID' />
            <form class="form-horizontal" id="formTeaSource" data-bind="with: NodeValue">

                <div class='control-group'>
                    <label class='control-label'>文件夹/文件名称:</label><div class='controls'>
                        <input type='text' id='txtbSSourceName' name='txtbSSourceName' placeholder='新文件夹名称' class='span10' data-bind='value:SSourceName' /><br />
                    </div>
                </div>

            </form>
        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">
                取消</button>
            <button id="btnSaveOrUpdateModel" class="btn btn-primary" aria-hidden="true" data-bind=" click: onDataAddingOrUpdate">
                确定</button>
        </div>
    </div>
    <!--end div content-->
</div>
<!--/详细TeaSource对话框-->



<!--Power tree-->
<div id="myModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="width: 600px;">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h3 id="myModalLabel"><strong>选择文件</strong></h3>
        </div>
        <div class="modal-body">
            <table class="table table-striped table-bordered table-hover checkboxs">
                <thead>
                    <tr>
                        <th>
                            <input type="hidden" id="pID" name="pID" data-bind="value:pID" /></th>
                        <th>文件名</th>
                    </tr>
                </thead>

                <tbody data-bind="foreach: Resource">
                    <tr>

                        <td>
                            <input type="checkbox" name="ResourceSelector" data-bind="value:$root.typeAndId(Id,FileType)" />
                            <input type="hidden" id="Parent" data-bind='value: IParentId' />
                            <input type="hidden" id="Level" data-bind='value: ILevel' />

                        </td>
                        <td>
                            <a href="#" style="color: black" data-bind='click:$root.GetPraviteFile'>
                                <i data-bind="css:$root.showFileType(FileType,IType)"></i>
                                <strong data-bind='text: SSourceName'></strong></a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <a href="#" data-bind="click:$root.GoToPrivateParent" style="margin-left: 10px; display: none" id="GoToPrivateParent">
                <h4 style="color: black; font-size: 16px;">返回上一级</h4>
            </a>
        </div>
        <div class="modal-footer">
            <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
            <button class="btn btn-primary" id="confirm_power">导入到教学资源</button>
        </div>
    </div>
</div>

<script type="text/javascript" src="~/Scripts/Z-tree/js/jquery.ztree.all-3.5.js"></script>
<script src="~/Scripts/customJS/zTreeCommon.js"></script>
<script type="text/javascript">

    //规范时间显示格式
    function date(date) {
        if (date == "" || date == null)
            return "";
        var dd = date.slice(0, 10);
        var ss = date.slice(11, 19);
        return dd + " " + ss;
    };

    //文件大小显示
    function Size(size) {
        if (size == "" || size == null)
            return;
        return size + "KB";
    };

    //发布状态显示
    function PublicState(state) {
        if (state == 1)
            return "发布";
        else
            return "未发布"
    }

    function DownloadPower(power) {
        if (power == 1)
            return "允许下载";
        else if (power == 0)
            return "不允许下载";
        else
            return "";
    }


    var gdViewModel;
    $(function () {

        var CourseID = $("#CouurseID").val();

        $._messengerDefaults = {
            extraClasses: 'messenger-fixed messenger-theme-future messenger-on-bottom messenger-on-right'
        }; //默认提示位置;


        var options = {

            dialogId: "TeaSourceMessageModal", //添加对话框

            pageSize: 20, //分页大小
            hidId: $("#txtbID"),
            SaveUpdateBtn: $("#btnSaveOrUpdateModel"),
            frmElement: $("#formTeaSource"),
            DefultGet: "POST",//数据传送方式

            //节点数据
            NodeValue: ko.observable({

                Id: 0,
                SSourceName: '',
                SUrl: '',
                IType: 0,
                ITeaSourceDictId: 0,
                DCreate: new Date(),
                SIp: '',
                SOwnerName: '',
                SRna: '',
                ISize: 0,
                SImageUrl: '',
                SIntro: '',
                IRate: 0,
                IParentId: 0,
                ILevel: 0,
                IOwnerUserId: "",
                FileType: 0,
                IIsDownload: 0,
                ITeaSourceId: 0,
            }),


            //用户列表Grid头部列名称
            headers: [
 { displayText: '文件名', value: 'SSourceName', width: 'auto' },
 { displayText: '大小', value: 'ISize', width: 'auto' },
 { displayText: '发布状态(点击修改)', value: 'IPublic', width: 'auto' },
 { displayText: '是否允许下载', value: 'IIsDownload', width: 'auto' },

            ],

            //默认的排序方式
            defaultOrderBy: "Id",
            //用户查询URL
            dataQueryUrlAccessor: function () {
                return "/api/Resource/GetPublicFile";
            },

            //添加用户URL
            dataAddUrlAccessor: function () { return "/api/Resource/AddPublicFile"; },
            dataUpdateAccessor: function (key) {
                return appendQueryString("/api/Resource/UpdatePublicFileName/", { keys: key });
            },
            dataNodeSelect: function (key) {

                return appendQueryString("/api/Resource/currentPublicFile", { keys: key });
            },

            dataDeleteAccessor: function (data) {
                var userIDs = "";
                var check = $("input:checked");  //得到所有被选中的checkbox
                check.each(function (i) {        //循环拼装被选中项的值                   

                    userIDs = userIDs + ',' + $(this).val();
                });
                //清除多余的分割号
                userIDs = dropRsplit(userIDs, ",");
                if (userIDs != "") {
                    return appendQueryString("/api/Resource/DeletePublicFile", { ids: userIDs });
                } else
                    return "";

            }
			, initEd: function () {
			    //gird初始化的后置事件 信息

			    gdViewModel.IParentID($("#recordParent").val());

			    if (($("#recordILevel").val() == 1) || ($("#IParentID").val() == "") && $("#ID").val() == "") {
			        $("#GoToParent").hide();

			    }
			    else {
			        $("#GoToParent").show();
			    }
			}
            , AddBefore: function () {
                var Id = $("#ID").val();
                var IParentId = $("#recordParent").val();
                var ILevel = $("#recordILevel").val();
                if (Id == "" || Id == null) {//0层
                    options.NodeValue().ICourseID = CourseID;
                    options.NodeValue().Id = 0;
                    options.NodeValue().IParentId = 0;
                    options.NodeValue().ILevel = 1;
                } else if (ILevel == "" || ILevel == null || ILevel == 0) {//空文件夹或只有文件的文件夹
                    options.NodeValue().ICourseID = CourseID;
                    options.NodeValue().Id = Id;
                    options.NodeValue().IParentId = Id;
                    options.NodeValue().ILevel = 0;
                } else {
                    options.NodeValue().ICourseID = CourseID;
                    options.NodeValue().Id = Id;
                    options.NodeValue().IParentId = IParentId;
                    options.NodeValue().ILevel = ILevel;
                }

                return true;
            }
            , showDlgBefore: function () { }
            , changeEnd: function () { }
            , showDetailEnd: function () { }

        };

        gdViewModel = new gridViewModel(options);
        gdViewModel.IParentID = ko.observable(0);
        gdViewModel.ID = ko.observable();
        gdViewModel.pID = ko.observable();

        //改变发布状态
        gdViewModel.ChangeState = function (data) {
            $.ajax({
                type: "post",
                url: "/api/Resource/ChangeState",
                data: { id: data.Id, fileType: data.FileType }
            }).done(function (data) {
                gdViewModel.init();
            })
        };
        gdViewModel.fontColor = function (data) {
            if (data == 0)
                return "red";
            else
                return "green";
        }
        //改变是否允许下载权限
        gdViewModel.ChangePower = function (data) {
            $.ajax({
                type: "POST",
                url: "/api/Resource/ChangePower",
                data: { id: data.Id, fileType: data.FileType },
            }).done(function (data) {
                gdViewModel.init();
            })
        };

        //合并文件Id和文件类型
        gdViewModel.typeAndId = function (id, type) {

            return id + ":" + type;
        }

        //获取文件夹下的文件
        gdViewModel.GetFile = function (data) {
            if (data.FileType == 1) {
                return;
            } else {
                gdViewModel.ID(data.Id);
                gdViewModel.IParentID(data.IParentID);
                gdViewModel.dataQueryUrlAccessor = function () {
                    return "/api/Resource/GetPublicFile";
                }
                gdViewModel.init();
            }
        };

        //获取私人文件夹下的文件
        gdViewModel.GetPraviteFile = function (data) {
            if (data.FileType == 1) {
                return;
            } else {
                $.ajax({
                    type: "post",
                    url: "/api/Resource/GetFile",
                    data: { ID: data.Id }
                }).done(function (res) {
                    gdViewModel.Resource(res.Data);
                    if ($("#Level").val() != 1)
                        $("#GoToPrivateParent").css("display", "inline");
                })
                var id = data.Id;
                gdViewModel.pID(id);
            }
        };



        //返回上一级
        gdViewModel.GoToParent = function () {

            gdViewModel.dataQueryUrlAccessor = function () {
                return "/api/Resource/PublicGoToParent";
            }
            gdViewModel.init();
        };

        //返回私人文件夹的上一级
        gdViewModel.GoToPrivateParent = function (data) {
            $.ajax({
                type: "post",
                url: "/api/Resource/GoToParent",
                data: { ID: $("#pID").val(), IParentID: $("#Parent").val(), CourseID: CourseID }
            }).done(function (res) {
                gdViewModel.Resource(res.Data);
                if ($("#Level").val() == 1)
                    $("#GoToPrivateParent").css("display", "none");
                else
                    $("#GoToPrivateParent").css("display", "inline");
            })
        }

        //图标样式转换
        gdViewModel.showFileType = function (fileType, IType) {

            if (fileType == 0) {//文件夹
                return "folder fa fa-folder ";
            } else {//文件

                switch (IType) {
                    case 1:
                        return "file fa fa-file-word-o";

                    case 2:
                        return "file fa fa-file-excel-o ";

                    case 3:
                        return "file fa fa-file-powerpoint-o ";
                    case 4:
                    case 5:
                        return "file fa fa-file-zip-o";
                    case 6:
                        return "file fa fa-file-picture-o";
                    case 7:
                        return "file fa a-file-sound-o";
                    default:
                        return "file fa fa-table";
                }

            }
        };


        gdViewModel.showImg = function (i, j) {
            if (i == 1)
                return "fa fa-check ";
            else if (i == 0)
                return "fa fa-times ";
            else
                return "";

        }


        //下载图标的显示
        gdViewModel.showUploadImag = function (fileType) {
            if (fileType == 0) {//文件夹
                return "";
            } else {//文件
                return "tag fa fa-download"
            }
        }
        //预览图标的显示
        gdViewModel.showPreImag = function (fileType, IType) {
            if (fileType == 0) {//文件夹
                return "";
            } else if (fileType == 1 && IType <= 3) {//可预览文件
                return "tag fa fa-search"
            }
        }

        //预览请求
        gdViewModel.ChangeView = function (data) {
            //$.ajax({
            //    type: "POST",
            //    url: "/api/Common/ShowOfficeDoc",
            //    data: { filePath: data.SUrl }
            //}).done(function (filePath) {
            //    window.open(filePath);
            //});//end ajax

            var Path = data.SUrl;
            var filePath = Path.substring(0, Path.lastIndexOf("."));
            var viewFile = filePath.replace("/UploadFile", "/ViewFile") + ".htm";
            window.open(viewFile);
        }

        //读取个人私有资源
        gdViewModel.Resource = ko.observableArray();
        $.ajax({
            type: "post",
            url: "/api/Resource/GetFile"
        }).done(function (data) {
            gdViewModel.Resource(data.Data);
        });


        gdViewModel.init();
        ko.applyBindings(gdViewModel);




        ///reload showUpdateModal
        gdViewModel.showUpdateModal = function (currUser) {
            var key = currUser.Id + ":" + currUser.FileType;
            gdViewModel.hidId.val(key);//修改
            gdViewModel.dialog.modal("show");
            $(':input', gdViewModel.frmElement)
            .not(':button, :submit, :reset')
            .val('')
            .removeAttr('checked')
            .removeAttr('selected');


            gdViewModel.showDetail(key);//显示信息

        };

        options.frmElement.validate({
            rules: {
                txtbSSourceName: { required: true }
            },
            messages: {
                txtbSSourceName: { required: '请为文件夹命名' }
            }
        });


        $("#confirm_power").click(function () {
            var currentid;
            var FileType = "";
            if ($("#IParentID").val() == "")
                currentid = $("#ID").val();
            else
                currentid = $("#IParentID").val();
            $('input[name="ResourceSelector"]:checked').each(function () {
                FileType = FileType + ',' + $(this).val();
            });
            //清除多余的分割号
            FileType = dropRsplit(FileType, ",");
            if (FileType != "") {
                $.ajax({
                    type: "post",
                    url: "/api/Resource/LeadingIn",
                    data: { FileType: FileType, Catalog: currentid, ICourseId: CourseID }
                }).done(function (data) {
                    window.location.reload();
                })
            }
            $("#confirm_power").attr("data-dismiss", "modal");
            $("#confirm_power").attr("aria-hidden", "true");
            return;
        })
    });

</script>

<!--/end grid code-->
